package swing.frame;

import api.MainServer;
import beans.Group;
import client.UserSession;
import com.syuchen.protocol.http.Response;
import model.ApiResultModel;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import swing.BaseFrame;
import swing.FrameManager;
import swing.customComp.CustomStyles;
import swing.customComp.HintJTextField;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MainFrame2 extends BaseFrame {

    public static void main(String[] args) {
        loadUiStyle();
        FrameManager.showLoginFrame();
        new MainFrame2().showFrame();
    }
    private static void loadUiStyle() {
        try {
            //解决切换输入法导致应用白屏的问题
            System.setProperty("sun.java2d.noddraw", "true");
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
            //使用BeautyEye开源库对整体UI进行美化
            //https://github.com/JackJiang2011/beautyeye
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            UIManager.put("RootPane.setupButtonVisible", false);
            //关闭窗口在不活动时的半透明效果
            BeautyEyeLNFHelper.translucencyAtFrameInactive = false;
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    JPanel leftPanel;
    JPanel centerPanel;
    JPanel rightPanel;

    List<Group> currentGroups = new ArrayList<>();
    DefaultListModel<String> groupListModel;

    @Override
    protected void initFrame() {
        this.setTitle("开源白板");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(960, 640));
        this.setResizable(false);
        this.pack();
        this.setLocationRelativeTo(null);
    }

    @Override
    protected void initAllComponent() {
        initLeft();
        initCenter();
        initRight();
    }

    @Override
    protected void addAllComponent() {
        this.add(leftPanel);
        this.add(centerPanel);
        this.add(rightPanel);
    }

    private void initLeft() {
        leftPanel = new JPanel();
        leftPanel.setLayout(null);
        leftPanel.setBounds(0,0,250,700);
//        leftPanel.setBackground(Color.CYAN);
        leftPanel.setBorder(new BevelBorder(1, Color.gray,Color.white));
        //搜索框
        HintJTextField searchField = new HintJTextField();
        searchField.setBounds(10, 20, 190, 25);
        searchField.setHintText("搜索..");
        searchField.setBackground(CustomStyles.color_EAEAEA);
        leftPanel.add(searchField);
        //创建群组按钮
        JButton createGroupBt = new JButton("+");
        createGroupBt.setBounds(210, 20, 25, 25);
        createGroupBt.setBackground(Color.LIGHT_GRAY);
        createGroupBt.setFont(new Font("宋体", Font.PLAIN, 10));
        leftPanel.add(createGroupBt);
        //群组列表
        JList<String> groupList = new JList<>();
        groupListModel = new DefaultListModel<>();
        groupList.setModel(groupListModel);

    }

    private void initCenter() {
        centerPanel = new JPanel();
    }

    private void initRight() {
        rightPanel = new JPanel();
    }

    public void flushGroupList() {
        try {
            Response response = MainServer.groupAction.get_group_list(UserSession.LoginUser());
            ApiResultModel<List<Group>> apiResult = ApiResultModel.parseListResult(response.getBody(), Group.class);
            currentGroups = apiResult.getResult();
            for (Group group : currentGroups) {
                groupListModel.addElement(group.getGroup_name());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
